package pl.koziolekweb.szkola.JYWatkiPool;

// JY-WatkiPool/WatkiEx.java
 
import java.util.concurrent.*;

public class WatkiEx extends Thread {

    ExecutorService pool = null;

    public static void main(String[] args) {
        new WatkiEx().start();
    }

    WatkiEx() {
        Runnable[] runs =
            {
                new Fibo(40L), new Fibo(39L),
                new Fibo(41L), new Fibo(38L),
                new Fibo(37L), new Fibo(42L),
            };
          // pula na cztery watki jednoczesne
        pool = Executors.newFixedThreadPool(4);

          // wrzucamy szesc watkow do wykonania
        for (Runnable r : runs)
            pool.execute(r);

          // nowych juz nie wolno dodawac
        pool.shutdown();
        System.out.println("Wykonano \"shutdown\"");
    }

    public void run() {
        while (!pool.isTerminated()) {
            try {
                Thread.sleep(1500);
            } catch (InterruptedException ignored) { }
            System.out.println("Jeszcze nie...");
        }
        System.out.println("Wszystko zrobione");
    }
}

class Fibo implements Runnable {

    long arg;

    static long fibon(long n) {
        return (n < 2) ? n : fibon(n-2) + fibon(n-1);
    }

    Fibo(long n) { arg = n; }

    public void run() {
        System.out.println("Fibo(" + arg + ") startuje");
        long res = fibon(arg);
        System.out.println("Fibo(" + arg + ") = " + res);
    }
}
